//
//  HTGoodsListVC.m
//  TruckUser
//
//  Created by Ting Han on 2017/10/30.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTGoodsListVC.h"
#import "HTFindGoodsDetialVC.h"
#import "HTGoodsSearchCell.h"
#import "HTRefreshMoreModel.h"
#import "HTGoodsSearchModel.h"

@interface HTGoodsListVC ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) HTRefreshMoreModel *refreshMoreModel;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *safeBottomHeight;
@property (nonatomic,assign) NSInteger pageNumber;

@end

@implementation HTGoodsListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configTableView];
}
- (void)configTableView{
    self.safeBottomHeight.constant = IPHONE_SAFEBOTTOMAREA_HEIGHT;
    self.pageNumber = 1;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.width = kHTScreenWidth;
    self.tableView.backgroundColor = kBackgroundColor;
    [self.tableView registerNib:[UINib nibWithNibName:@"HTGoodsSearchCell" bundle:nil] forCellReuseIdentifier:GoodSearchCell];
    self.tableView.estimatedRowHeight =0;
    self.tableView.estimatedSectionHeaderHeight =0;
    self.tableView.estimatedSectionFooterHeight =0;
    [self configTableHeader];
    [self configTableFooter];
    self.tableView.mj_header = self.header;
    self.tableView.mj_footer = self.footer;
    self.noDataView = [[HTNoDataView alloc]initWithFrame:self.tableView.bounds];
    self.noDataView.showMessage = @"暂无货源";
}


#pragma mark --- lazyLoad

//货源数据
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}


#pragma mark --- tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    [tableView tableViewDisplayView:self.noDataView ifNecessaryForRowCount:self.dataSource.count];
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HTGoodsSearchCell *cell  = [tableView dequeueReusableCellWithIdentifier:GoodSearchCell forIndexPath:indexPath];
    cell.goodsSearchModel = self.dataSource[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HTFindGoodsDetialVC *find = [[HTFindGoodsDetialVC alloc]init];
    find.goodsSearchModel = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:find animated:YES];
    
}

#pragma mark --- 网络请求
//货源搜索数据
- (void)netLoadGoodsSearchWithIndex:(NSInteger)index{
   
    [HTHttpRequestManager requestOrderlineSearchWithId:self.ID WithPage:[NSString stringWithFormat:@"%ld",index] success:^(id  _Nullable responseObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (!responseObject) {
            return ;
        }
        DBLog(@"专线搜索列表:%@",responseObject);
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            return;
        }
        if (self.pageNumber == 1) {
            //代表是刷新的
            [self.dataSource removeAllObjects];
        }
        NSArray *result = responseObject[@"data"][@"data"];
        NSMutableArray *dataResult = [HTGoodsSearchModel mj_objectArrayWithKeyValuesArray:result];
        [self.dataSource addObjectsFromArray:dataResult];
        self.refreshMoreModel = [HTRefreshMoreModel mj_objectWithKeyValues:responseObject[@"data"]];
        NSString *currentPage = self.refreshMoreModel.current_page;
        self.pageNumber = [currentPage integerValue];
        [self.tableView reloadData];

    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}
- (void)loadMoreData{
    [self.tableView.mj_header endRefreshing];
    if (self.dataSource.count == 0) {
        [self.tableView.mj_footer endRefreshing];
        self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        return;
    }
    self.pageNumber ++;
    if (self.refreshMoreModel && [self.refreshMoreModel.total integerValue] < self.pageNumber) {
        [self.tableView.mj_footer endRefreshing];
        self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        return;
    }
    [self netLoadGoodsSearchWithIndex:self.pageNumber];
}
- (void)loadNewData{
    self.pageNumber = 1;
    [self.tableView reloadData];
    [self netLoadGoodsSearchWithIndex:1];
}


@end
